package com.gomore.opple.module.orderpay.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gomore.opple.R;
import com.gomore.opple.model.Action;

import java.util.List;

/**
 * Created by asus on 2017/5/22.
 */
public class ConditionAdapter extends BaseAdapter {

    Context mContext;
    List<Action> actions;

    public ConditionAdapter(Context context, List<Action> actionList) {
        mContext = context;
        actions = actionList;
    }

    @Override
    public int getCount() {
        return actions.size();
    }

    @Override
    public Object getItem(int position) {
        return actions == null ? 0 : actions.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.condition_item, parent, false);
        RelativeLayout item = (RelativeLayout) convertView.findViewById(R.id.item);
        TextView name = (TextView) convertView.findViewById(R.id.name);
        ImageView select = (ImageView) convertView.findViewById(R.id.select);

        Action action = actions.get(position);
        if (action.getDescription() != null) {
            name.setText(action.getDescription());
        }
        if (action.isSelect()) {
            item.setEnabled(false);
            select.setVisibility(View.VISIBLE);
        } else {
            item.setEnabled(true);
            select.setVisibility(View.GONE);
        }
        item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //置为未选择
                setUnSelect(actions);
                selectCondition(actions, actions.get(position));
                notifyDataSetChanged();
            }
        });
        return convertView;
    }

    //对促销条件进行选择
    private void selectCondition(List<Action> list, Action action) {
        action.setIsSelect(true);
        String conflict = action.getConflicts().toString();
        for (int i = 0; i < list.size(); i++) {
            if (!conflict.contains(list.get(i).getType()) && !list.get(i).getType().equals(action.getType())) {
                //如果冲突为空，直接选择
                if (list.get(i).getConflicts().size() == 0) {
                    list.get(i).setIsSelect(true);
                } else {
                    for (int j = 0; j < list.get(i).getConflicts().size(); j++) {
                        String conflictStr = list.get(i).getConflicts().toString();
                        if (!conflictStr.contains(action.getType())) {
                            list.get(i).setIsSelect(true);
                        }
                    }
                }
            }
        }
    }

    private void setUnSelect(List<Action> list) {
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setIsSelect(false);
            list.get(i).setIsUsed(false);
        }
    }
}
